<template>
    <el-card shadow="always" :body-style="{ padding: '20px' }">
        <el-button type="primary" size="default" icon="el-icon-plus" @click="reset" v-if="$hasBP('btn.Trademark.add')">添加</el-button>
        <el-table v-loading="loading" border stripe style="margin-top:20px" :data="this.tradmarkList.records">
            <el-table-column label="序号" width="80" type="index">
            </el-table-column>
            <el-table-column label="品牌名称" prop="tmName">
            </el-table-column>
            <el-table-column label="品牌log">
                <template slot-scope="{row,$index}">
                    <img :src="row.logoUrl" style="width:100px;height:60px">
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="{row,$index}">
                    <el-button type="warning" size="small" icon="el-icon-edit" v-if="$hasBP('btn.Trademark.update')" @click="upData(row)">修改</el-button>

                    <el-button type="danger" size="small" icon="el-icon-delete" v-if="$hasBP('btn.Trademark.remove')" @click="delData(row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination @size-change="handlersize" @current-change="gettardmarkList"
            style="margin-top:20px ; text-align:center" :page-sizes="[3, 5, 7, 9]" :current-page="page"
            :page-size="limit" layout="prev, pager, next, jumper, ->, sizes, total" :total="this.tradmarkList.total">
        </el-pagination>



        <!-- 添加或修改弹框 -->
        <el-dialog title="添加品牌" :visible.sync="dialogVisible">
            <el-form style="width: 80%" label-width="100px" :inline="false" size="normal" :model="rule" :rules="rules"
                ref="ruleForm">
                <el-form-item label="品牌名称" prop="tmName">
                    <el-input v-model="rule.tmName"></el-input>
                </el-form-item>
                <el-form-item label="品牌LOG" prop="logoUrl">
                    <el-upload class="avatar-uploader" :action="`${$BASE_API}/admin/product/fileUpload`"
                        :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img v-if="rule.logoUrl" :src="rule.logoUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="addOrupData">确 定</el-button>
            </span>
        </el-dialog>

    </el-card>

</template>

<script>
export default {
    data() {
        return {
            // 加载效果
            loading:false,
            // 第几页
            limit: 3,
            // 每页条数
            page: 1,
            // 商品数据
            tradmarkList: [],
            // 显示于隐藏的弹出框
            dialogVisible: false,
            rule: {
                // 图片
                logoUrl: '',
                // 添加或修改的名字
                tmName: ''
            },
            rules: {
                tmName: [
                    { required: true, message: '添加的名称不能为空', trigger: 'blur' },
                    { min: 3, message: '长度不能小于三个', trigger: 'blur' }
                ],
                logoUrl: [
                    { required: true, message: '图片不能为空', trigger: 'blur' }
                ],
            }

        }
    },
    // 挂载前调用函数
    beforeMount() {
        // console.log(this.$API);
        this.gettardmarkList()
    },
    methods: {
        // 获取列表数据
        async gettardmarkList(page = 1) {
            this.loading=true
            this.page = page
            let result = await this.$API.tradmark.reqTardmarkList(this.page, this.limit)
            // console.log(result);
            this.tradmarkList = result.data
            this.loading=false
        },
        handlersize(limit) {
            this.limit = limit
            this.gettardmarkList()
        },
        handleAvatarSuccess(res, file) {
            this.rule.logoUrl = res.data
        },
        // 上传图片的约束
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        // 点击修改按钮
        upData(row) {
            console.log(row);
            // 结构赋值
            this.rule = { ...row }
            // 显示弹框
            this.dialogVisible = true
        },
        // 取消添加或修改按钮
        cancel() {
            this.dialogVisible = false
            //  清空表单数据
            this.rule = {
                // 图片
                logoUrl: '',
                // 添加或修改的名字
                tmName: ''
            }
        },

        // 确定添加或修改按钮
        addOrupData() {
            //   表单校验
            this.$refs['ruleForm'].validate(async (valid) => {
                if (valid) {
                    //整理参数
                    const trademark = this.rule
                    let result
                    if (trademark.id) {
                        result = await this.$API.tradmark.requpdata(trademark)
                    } else {
                        result = await this.$API.tradmark.reqaddData(trademark)
                    }
                    console.log(result);
                    // 如果成功, 提示成功, 并获取新的列表显示
                    this.$message.success(result.message || '保存成功!')
                    // 关闭当前dialog
                    this.dialogVisible = false
                    //  更新页面
                    this.gettardmarkList(trademark.id ? this.page : 1)
                    //  清空表单数据
                    this.rule = {
                        // 图片
                        logoUrl: '',
                        // 添加或修改的名字
                        tmName: ''
                    }
                } else {
                    this.$message.danger(trademark.id ? '修改失败' : '添加失败')
                }
            })

        },

        // 点击添加按钮重置表单
        reset() {
            //  清空表单数据
            this.rule = {
                // 图片
                logoUrl: '',
                // 添加或修改的名字
                tmName: ''
            }
            // 对话框显示
            this.dialogVisible=true

            this.$nextTick(()=>{
                this.$refs.ruleForm.clearValidate()            })
        },

        // 删除按钮
        async delData(row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$API.tradmark.reqDelData(row.id)
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                //  更新页面
                this.gettardmarkList(this.page

                )
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        }
    }
};



</script>

<style >
.avatar-uploader .el-upload {
    border: 1px dashed #8c939d;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>